<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../../../assets/heima.png"
             alt="图片无法查看" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info"
                 @click="loginOut()">退出账号</el-button>
    </el-header>

    <!-- 页面主体区域 -->
    <el-container>
      <!-- 左侧导航栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!--  -->
        <div class="toggle-button"
             @click="toggleCollapse()">|||</div>

        <!-- 侧边栏菜单导航区 -->
        <!-- 
          :defaultActive="this.$route.path"--》点击item列表文字高亮
        -->
        <el-menu background-color="#333744"
                 text-color="#fff"
                 active-text-color="#409bff"
                 :unique-opened="true"
                 :collapse="isCollapse"
                 :collapse-transition="false"
                 router
                 :defaultActive="this.$route.path">
          <!-- :unique-opened="true":每次只打开一个下拉菜单 -->
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''"
                      v-for="item in menuslist"
                      :key="item.id">
            <template slot="title">
              <!-- 图标 -->
              <i :class="iconsobj[item.id]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
              <!-- 一级菜单模板区 -->
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path + ''"
                          v-for="subItem in item.children"
                          :key="subItem.id">
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ subItem.authName }}</span>
                <!-- 一级菜单模板区 -->
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主要内容区域 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data () {
    return {
      // 左侧菜单数据
      menuslist: [],
      iconsobj: {
        "125": "iconfont icon-yonghu",
        "103": "iconfont icon-quanxian",
        "101": "iconfont icon-shangpingouwudai2",
        "102": "iconfont icon-icon",
        "145": "iconfont icon-tongji"
      },
      isCollapse: false // 是否折叠，true为折叠，false为不折叠
    };
  },
  created () {
    this.getMenuList();
  },
  methods: {
    loginOut () {
      // 退出登录
      // 清除sessionStorage中的token并且跳转到login页面
      window.sessionStorage.clear;
      this.$router.push("/login");
    },
    async getMenuList () {
      // 获取左侧菜单数据
      const { data: res } = await this.$ajax.get("menus");
      if (res.meta.status != 200) {
        return this.$message.err("获取左侧列表错误");
      }
      return (this.menuslist = res.data);
    },
    toggleCollapse () {
      // 点击按钮，切换菜单的折叠与展开
      this.isCollapse = !this.isCollapse;
    }
  }
};
</script>

<style lang="less" scoped>
.home-container {
  height: 1080px;
}

.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}

.el-aside {
  background-color: #333744;
}

.el-main {
  background-color: #eaedf1;
}

.iconfont {
  margin-right: 10px;
}

.el-menu {
  border: 0;
}

.toggle-button {
  background-color: #4a5064;
  font-size: 10px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
